package com.situ.mall.order.controller;


import com.situ.mall.common.util.Result;
import com.situ.mall.order.pojo.vo.CartVO;
import com.situ.mall.order.service.ICartService;
import com.situ.mall.pojo.entity.order.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/order/cart")
public class CartController {
	@Autowired
	private ICartService cartService;
	
	@PostMapping("/add")
	public Result<CartVO> add(@RequestBody Cart cart) {
		System.out.println("添加信息："+cart);
		/*Result<Cart> result = new Result<>();
		Cart c =cartService.add(cart,result);
		result.setData(c);
		result.setMsg("添加成功！");
		System.out.println(result.getCode());
		return result;*/
		
		CartVO c =cartService.add(cart);
		return Result.ok("添加成功！",c);
	}
	
	@GetMapping("/selectByUserId/{userId}")
	public Result<List<CartVO>> selectByUserId(@PathVariable Long userId){
		List<CartVO> list=cartService.selectByUserId(userId);
		return Result.ok(list);
	}
	
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id){
		cartService.removeById(id);
		return Result.ok("删除成功！");
	}
	
	@PutMapping("/update")
	public Result update(@RequestBody Cart cart){
		cartService.updateById(cart);
		return Result.ok("修改成功！");
	}
}

